package com.dl698.axdr;

import com.dl698.asn1.axdr.AxdrType;
import com.dl698.asn1.axdr.AxdrEnum;
import com.dl698.util.ReverseByteArrayInputStream;
import com.dl698.util.ReverseByteArrayOutputStream;

/**
 * CSD类
 * 实现采集存储描述符的编解码
 * 
 * @author wind
 * @version 1.1.1
 */
public class CSD implements AxdrType {

    private byte[] dataCode = null;
    private CSDChoice choice = CSDChoice._ERR_NONE_SELECTED;
    private OAD oad = null;
    private ROAD road = null;

    /**
     * 默认构造函数
     */
    public CSD() {
    }

    /**
     * 设置数据编码
     * 
     * @param dataCode 数据编码
     */
    public void setDataCode(byte[] dataCode) {
        this.dataCode = dataCode;
    }

    /**
     * 设置OAD
     * 
     * @param oad 对象属性描述符
     */
    public void setOad(OAD oad) {
        this.resetChoices();
        this.choice = CSDChoice.OAD;
        this.oad = oad;
    }

    /**
     * 设置ROAD
     * 
     * @param road 读取对象属性描述符
     */
    public void setRoad(ROAD road) {
        this.resetChoices();
        this.choice = CSDChoice.ROAD;
        this.road = road;
    }

    @Override
    public int decode(ReverseByteArrayInputStream input) {
        int codeLength = 0;
        AxdrEnum choosen = new AxdrEnum();
        choosen.setConst();

        codeLength += choosen.decode(input);
        this.resetChoices();
        this.choice = CSDChoice.fromValue(choosen.getValue());

        if (this.choice == CSDChoice.OAD) {
            this.oad = new OAD();
            codeLength += this.oad.decode(input);
            return codeLength;
        }

        if (this.choice == CSDChoice.ROAD) {
            this.road = new ROAD();
            codeLength += this.road.decode(input);
            return codeLength;
        }
        throw new RuntimeException("Error decoding AxdrChoice: Identifier matched to no item.");
    }

    @Override
    public int encode(ReverseByteArrayOutputStream output) {
        if (this.dataCode != null) {
            for (int i = this.dataCode.length - 1; i >= 0; i--) {
                try {
                    output.write(this.dataCode[i]);
                } catch (java.io.IOException e) {
                    throw new RuntimeException(e);
                }
            }
            return this.dataCode.length;
        }

        if (this.choice == CSDChoice._ERR_NONE_SELECTED) {
            throw new RuntimeException("Error encoding AxdrChoice: No item in choice was selected.");
        }

        int codeLength = 0;

        if (this.choice == CSDChoice.ROAD) {
            codeLength += this.road.encode(output);
            AxdrEnum c = new AxdrEnum();
            c.setMinMaxVal(1);
            codeLength += c.encode(output);
            return codeLength;
        }

        if (this.choice == CSDChoice.OAD) {
            codeLength += this.oad.encode(output);
            AxdrEnum c = new AxdrEnum();
            c.setMinMaxVal(0);
            codeLength += c.encode(output);
            return codeLength;
        }
        throw new RuntimeException("Error encoding AxdrChoice: No item in choice was encoded.");
    }

    /**
     * 获取选择索引
     * 
     * @return 选择索引
     */
    public CSDChoice getChoiceIndex() {
        return this.choice;
    }

    /**
     * 获取选择
     * 
     * @return 选择
     */
    public CSDChoice getChoice() {
        return this.choice;
    }

    /**
     * 获取对象属性描述符
     * 
     * @return 对象属性描述符
     */
    public OAD getOad() {
        return oad;
    }

    /**
     * 获取读取对象属性描述符
     * 
     * @return 读取对象属性描述符
     */
    public ROAD getRoad() {
        return road;
    }

    /**
     * 编码并保存
     * 
     * @param encodingSizeGuess 编码大小预估
     */
    public void encodeAndSave(int encodingSizeGuess) {
        ReverseByteArrayOutputStream revOStream = new ReverseByteArrayOutputStream();
        revOStream.setBufSize(encodingSizeGuess);
        encode(revOStream);
        this.dataCode = revOStream.getArray();
    }

    /**
     * 重置选择
     */
    public void resetChoices() {
        this.choice = CSDChoice._ERR_NONE_SELECTED;
        this.oad = null;
        this.road = null;
    }

    @Override
    public String toString() {
        if (this.choice == CSDChoice.OAD) {
            if (this.oad != null) {
                return "choice: {oad: " + this.oad + "}";
            } else {
                return "choice is oad but oad is null";
            }
        } else if (this.choice == CSDChoice.ROAD) {
            if (this.road != null) {
                return "choice: {road: " + this.road + "}";
            } else {
                return "choice is road but road is null";
            }
        } else {
            return "unknown";
        }
    }
}